package com.hrm.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hrm.dto.Response;
import com.hrm.dto.ResponseDTO;
import com.hrm.entity.RoomRepair;
import com.hrm.mapper.RoomRepairMapper;
import com.hrm.util.HutoolExcelUtil;
import com.hrm.vo.RoomRepairVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoomRepairService extends ServiceImpl<RoomRepairMapper, RoomRepair> {

    @Resource
    private RoomRepairMapper roomrepairMapper;

    public ResponseDTO add(RoomRepair roomRepair) {
        if (save(roomRepair)) {
            return Response.success();
        }
        return Response.error();
    }

    public ResponseDTO deleteById(Integer id) {
        if (removeById(id)) {
            return Response.success();
        }
        return Response.error();
    }

    public ResponseDTO deleteBatch(List<Integer> ids) {
        if (removeBatchByIds(ids)) {
            return Response.success();
        }
        return Response.error();
    }

    public ResponseDTO findById(Integer id) {
        RoomRepair roomRepair = getById(id);
        if (roomRepair != null) {
            return Response.success(roomRepair);
        }
        return Response.error();
    }

    public ResponseDTO list(Integer current, Integer size, String roomName) {
        IPage<RoomRepairVO> config = new Page<>(current, size);
        if (roomName == null) {
            roomName = "";
        }
        IPage<RoomRepairVO> page = this.roomrepairMapper.listRoomRepairVO(config, roomName);
        // 将响应数据填充到map中
        Map map = new HashMap();
        map.put("pages", page.getPages());
        map.put("total", page.getTotal());
        map.put("list", page.getRecords());
        return Response.success(map);
    }

    /**
     * 数据导出
     *
     * @param response
     * @return
     */
    public ResponseDTO export(HttpServletResponse response) throws IOException {
        List<RoomRepair> list = list();
        HutoolExcelUtil.writeExcel(response, list, "会议室报修记录表", RoomRepair.class);
        return Response.success();
    }

    public ResponseDTO findByStaffId(Integer current, Integer size, Integer id) {
        IPage<RoomRepairVO> config = new Page<>(current, size);
        IPage<RoomRepairVO> page = this.roomrepairMapper.listRoomRepairVOByStaffId(config, id);
        // 将响应数据填充到map中
        Map map = new HashMap();
        map.put("pages", page.getPages());
        map.put("total", page.getTotal());
        map.put("list", page.getRecords());
        return Response.success(map);
    }

    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO edit(RoomRepair roomRepair) {
        if (updateById(roomRepair)) {
            return Response.success();
        }
        return Response.error();
    }
}




